/*	Copyright 2007 - Xavier Baro (xbaro@cvc.uab.cat)

	This file is part of eapmlib.

    Eapmlib is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or any 
	later version.

    Eapmlib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "EAPMLearner.h"

Evolutive::CEAPMLearner::CEAPMLearner(void) : CEvolLearner(),m_NumUpdatable(0),m_EvolStrat(Evolutive::EVOL_UPDATE),m_NumEstimation(0),m_EstimationPeriod(0)
{	
	//! Set the default parameters
	m_NumUpdatable=2;
	m_NumEstimation=50;
	m_EstimationPeriod=5;
}

Evolutive::CEAPMLearner::~CEAPMLearner(void)
{
}

void Evolutive::CEAPMLearner::InitLearner(void)
{
	int NumVars;

	//! Call the initialization for the base class
	CEvolLearner::InitLearner();

	//! Obtain the problem description from the Evaluator
	m_Evaluator->GetProblemDesc(NumVars);

	//! Initialize the probability model
	m_ProbModel->InitializeModel(NumVars,m_Population);
}

bool Evolutive::CEAPMLearner::ApplyStopCriteria(void)
{
	//! Call the initialization for the base class
	if(CEvolLearner::ApplyStopCriteria())
		return true;

	//! If the default criteria is false, check for additional criterias
	return m_ProbModel->IsStatic(m_Tolerance);
	
}

void Evolutive::CEAPMLearner::SetEvolStrategy(Evolutive::EVOL_STRATEGY EvolStrat)
{
	m_EvolStrat=EvolStrat;
}
		
void Evolutive::CEAPMLearner::UpdateModel(void)
{
	register int i;
	CScoreStruct Score;

	//! Update the model using the best individuals
	for(i=0;i<m_NumUpdatable;i++)
	{
		//! Obtain the score and index for the best individuals
		Score=m_Population.GetSortedScore(i);
		
		//! Update the model
		m_ProbModel->Update(m_Population[Score.Idx]);
	}
}

void Evolutive::CEAPMLearner::EstimateModel(void)
{
	//! Estimate a new model from data
	m_ProbModel->Estimate(m_NumEstimation,m_Population);	
}

void Evolutive::CEAPMLearner::NextGeneration(void)
{
	//! Depending on the evolution strategy perform the appropiate actions
	switch(m_EvolStrat)
	{
	case EVOL_UPDATE:
		UpdateModel();
		break;
	case EVOL_ESTIMATE:
		EstimateModel();
		break;
	case EVOL_MIXED:
		//! If we are in an estimation cycle call the estimation function, otherway update
		if(m_IterNum%m_EstimationPeriod)
			UpdateModel();
		else
			EstimateModel();		
		break;
	}
		
	//! Generate a new population
	m_ProbModel->NewPopulation(m_Population,m_Population[0].GetCodingMethod(),m_Evaluator);
}

void Evolutive::CEAPMLearner::SetProbModel(Evolutive::CProbModel *ProbModel)
{
	m_ProbModel=ProbModel;
}

void Evolutive::CEAPMLearner::SetNumUpdate(int NumIndividuals)
{
	m_NumUpdatable=NumIndividuals;
}

void Evolutive::CEAPMLearner::SetModelTolerance(double Tolerance)
{
	m_Tolerance=Tolerance;
}

void Evolutive::CEAPMLearner::SetNumEstimation(int NumIndividuals)
{
	m_NumEstimation=NumIndividuals;
}

void Evolutive::CEAPMLearner::UseAprioriPopulation(void)
{
	//! If no a priory population is available ends
	if(!m_InitialPopulation)
		return;

	//! Estimate a model from the initial population
	m_ProbModel->Estimate(m_InitialPopulation->GetPopulationSize(),m_InitialPopulation[0]);

	//! Generate a new population
	m_ProbModel->NewPopulation(m_Population,m_InitialPopulation->GetChromosomePtr(0)->GetCodingMethod(),m_Evaluator);
}
